package ch.sv7.tool.dbmanager.db;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import ch.sv7.tool.dbmanager.db.difference.NotPrimaryKeyDifference;
import ch.sv7.tool.dbmanager.db.difference.PrimaryKeyDifference;
import ch.sv7.tool.dbmanager.db.difference.SchemaDifferences;

public class ColumnSchema implements Serializable, SchemaElement {

	private static final long serialVersionUID = 1L;

	protected TableSchema table;

	protected ColumnType type;

	protected String name;

	protected boolean primaryKey;
	
	/*
	 * Referenced indexes and foreign keys
	 */
	protected Set referencedIndexes = new HashSet();
	protected Set referencedForeignKeys = new HashSet();

	public ColumnSchema(TableSchema table, String name, ColumnType type, boolean primaryKey) {
		this.table = table;
		this.name = name;
		this.type = type;
		this.primaryKey = primaryKey;
	}

	public ColumnSchema cloneColumn() {
		return new ColumnSchema(table, name, type.cloneType(), primaryKey);
	}
	public void addReferencedIndex(IndexSchema index) {
		referencedIndexes.add(index);
	}
	
	public Set getReferencedIndexes() {
		return referencedIndexes;
	}
	
	public void addReferencedForeignKey(ForeignKeySchema fk) {
		referencedForeignKeys.add(fk);
	}
	
	public Set getReferencedForeignKeys() {
		return referencedForeignKeys;
	}

	public String getSchemaIdentifier() {
		return "Columm: " + getTable().getName() + "." + getName();
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public boolean isPrimaryKey() {
		return primaryKey;
	}

	public ColumnType getType() {
		return type;
	}
	/**
	 * @param type the type to set
	 */
	public void setType(ColumnType type) {
		this.type = type;
	}

	public TableSchema getTable() {
		return table;
	}

	public void computeDifferencesFromActual(SchemaDifferences diffs, ColumnSchema actual) {

		if (isPrimaryKey() && !actual.isPrimaryKey())
			diffs.addDifference(this, new NotPrimaryKeyDifference(this, actual));
		
		if (!isPrimaryKey() && actual.isPrimaryKey())
			diffs.addDifference(this, new PrimaryKeyDifference(this, actual));
		
		if (!getType().equals(actual.getType())){
			getType().computeDifferencesFromActual(diffs, this, actual);
		}
	}

	public void setPrimaryKey(boolean primaryKey) {
		this.primaryKey = primaryKey;
	}

	public void forceLowerCase() {
		name = name.toLowerCase();
	}

	public void forceUpperCase() {
		name = name.toLowerCase();
	}

	public String toString() {
		String result = "  Column " + name  
			+ " " + (primaryKey ? "[PK]" : "")
			+ "[" + type.toString() +"]" + "\n";
		return result;
	}

	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + (primaryKey ? 1231 : 1237);
		result = prime * result + ((type == null) ? 0 : type.hashCode());
		return result;
	}

	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final ColumnSchema other = (ColumnSchema) obj;
		
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (primaryKey != other.primaryKey)
			return false;
		if (type == null) {
			if (other.type != null)
				return false;
		} else if (!type.equals(other.type))
			return false;
		return true;
	}
	
	
	
}
